package com.ruifu.pls.service;

import com.ruifu.pls.entity.DocOrderDetails;
import com.ruifu.pls.entity.DocOrderHeader;
import com.ruifu.pls.entity.DocOrderPackingSummary;
import com.ruifu.pls.model.GLCallInputParam;
import com.ruifu.pls.model.odoReturn.GLDocOrderHeader;
import com.ruifu.pls.model.odoReturnConform.GLHeader;
import com.ruifu.pls.repository.GDocOrderDetailsRepository;
import com.ruifu.pls.repository.GDocOrderHeaderRepository;
import com.ruifu.pls.repository.GGLDocOrderPackingRepository;
import com.ruifu.pls.utils.HttpUtils;
import com.ruifu.pls.utils.SighUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;

/**
 * ********************************************************
 * * @desc  ：
 * * @author  ${user}
 * * @date    ${date}
 * * @version v1.0
 * * ${tags}
 * *******************************************************
 */
@Service
public class DocOrderHeaderService {


    private org.slf4j.Logger logger = LoggerFactory.getLogger(DocOrderHeaderService.class);

    @Autowired
    GDocOrderHeaderRepository docOrderHeaderRepository;
    @Autowired
    GDocOrderDetailsRepository docOrderDetailsRepository;
    @Autowired
    GGLDocOrderPackingRepository gglDocOrderPackingRepository;
    //出库单确认接口
   // @Transactional
    public  void postDocOrderHeaderToGlin() {
        DocOrderHeader docOrderHeader = docOrderHeaderRepository.findByDocOrderHeader();
        if (docOrderHeader!=null){


        String orderno = docOrderHeader.getOrderno();
            List<DocOrderDetails> glDocOrderDetailsList = docOrderDetailsRepository.findByDocOrderDetails(orderno);
            List<DocOrderPackingSummary>  docOrderPackingSummariesList = gglDocOrderPackingRepository.findByDocOrderPacking(orderno);
            GLDocOrderHeader glDocOrderHeader = docOrderHeader.glDocOrderHeader;
            for (int i = 0; i < glDocOrderDetailsList.size(); i++) {
                glDocOrderHeader.orderLines.add(glDocOrderDetailsList.get(i).glDocOrderDetails);
            }
            for (int k = 0; k < docOrderPackingSummariesList.size(); k++){
                glDocOrderHeader.packages.add(docOrderPackingSummariesList.get(k).glDocOrderPackingSummary);
            }

            GLDocOrderHeader glReturnAsnInputRaram = glDocOrderHeader;

            GLCallInputParam glCallInputParam = new GLCallInputParam();
            glCallInputParam.setData(glDocOrderHeader);
            glCallInputParam.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam)));
            glCallInputParam.setMethod("g2matrix.outboundorder.unOutBoundOK");

            logger.info(glCallInputParam.toString());

                    try {
                        String responsse =HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam.toString());                    //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            docOrderHeaderRepository.findByReturnStatusUpdate(orderno);
                        } else {
                            docOrderHeaderRepository.findByReturnStatusUpdateEorro(orderno);
                            logger.info(responsse);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    }
       //销售订单确认接口
       @Transactional
        public void DocOrderConformToGlin(){
            DocOrderHeader docOrderHeader = docOrderHeaderRepository.findByDocOrderHeaderForConform();
            if(docOrderHeader!=null){
            String orderno = docOrderHeader.getOrderno();
            List<DocOrderPackingSummary>  docOrderPackingSummariesList1 = gglDocOrderPackingRepository.findByDocOrderPacking(orderno);

            List<DocOrderDetails> glDocOrderDetailsList1 = docOrderDetailsRepository.findByDocOrderDetails(orderno);
            GLHeader glHeader = docOrderHeader.glHeader;
            for (int i = 0; i < glDocOrderDetailsList1.size(); i++) {
                glHeader.orderLines.add(glDocOrderDetailsList1.get(i).glOrderLines);
            }
            for (int k = 0; k < docOrderPackingSummariesList1.size(); k++){
                glHeader.packages.add(docOrderPackingSummariesList1.get(k).glPackages);
            }
            GLHeader glReturnAsnInputRaram1 = glHeader;

            GLCallInputParam glCallInputParam1 = new GLCallInputParam();
            glCallInputParam1.setData(glHeader);
            glCallInputParam1.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam1)));
            glCallInputParam1.setMethod("g2matrix.saleorder.unOrderCodeOK");

                    logger.info(glCallInputParam1.toString());
                    try {
                        String responsse =HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam1.toString());                    //System.out.println(responsse);
                        logger.info(responsse);
                        if (responsse.contains("\"code\":0")) {
                            docOrderHeaderRepository.findByReturnStatusUpdate(orderno);
                            logger.info(responsse);
                        } else {
                            docOrderHeaderRepository.findByReturnStatusUpdateEorro(orderno);
                            logger.info(responsse);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
       }
}
